package org.ruizhijie.i.impl;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ruizhijie.config.TopicQueue;
import org.ruizhijie.i.ISubcriber;
import org.ruizhijie.util.CharUtil;

public class OutTcpSubcriberImpl implements ISubcriber{
	private Logger log = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	private TopicQueue tq;
	private String topic;
	private Map<String, Channel> map;

	
	public OutTcpSubcriberImpl(TopicQueue tq,String topic,Map<String, Channel> map){
		this.tq=tq;
		this.topic=topic;
		this.map=map;
		this.subcribe();
	}
	
	@Override
	public void subcribe() {
		// TODO Auto-generated method stub
		tq.subcribe(this, topic);
	}

	@Override
	public void unSubcribe() {
		tq.unSubcribe(this, topic);
	}

	@Override
	public void onMessage(byte[] msg) {
		for (String key : map.keySet()) {
			map.get(key).writeAndFlush(Unpooled.copiedBuffer(msg));
			log.info("-----out-to-tcp-----"+CharUtil.bytesToHexString(msg));
		}
	}

}
